<template>
  <div class="full-box shadow-box padding-box">
    <OperateCommon
      :showSave="true"
      :showBack="true"
      @save="save"
      @back="linkTo('/PolicyList')"
    >
    </OperateCommon>
    <div class="c-form-padding">
      <el-form
        class="c-form"
        :model="form"
        ref="form"
        :rules="rules"
        label-width="100px"
        :inline="false"
      >
        <el-form-item label="信息标题" prop="info_title">
          <el-input v-model="form.info_title" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="所属栏目" prop="info_column">
          <el-select v-model="form.info_column" clearable placeholder="">
            <el-option
              v-for="item in lanmu"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属类型" prop="type" v-if="form.info_column=='政策法规'">
          <el-select v-model="form.type" clearable placeholder="">
            <el-option
              v-for="item in types"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布部门" prop="publish_unit">
          <el-input
            style="width: 300px; margin-right: 10px"
            v-model="form.publish_unit"
            placeholder=" "
          ></el-input>
        </el-form-item>
        <el-form-item label="发布日期" prop="publish_date">
          <el-date-picker
            v-model="form.publish_date"
            type="date"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item style="position:relative" prop="t_news" label="上传附件">
          <CommonUpload
            tableName="t_news"
            :accept="form.file_type"
            :showUploadBtn="true"
            :disabled="allowEdit()"
            :pid="form.id > 0 ? form.id : null"
            useType="default"
          ></CommonUpload>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import OperateCommon from "@/components/Common/OperateCommon.vue";
import DeptCascader from "@/components/Common/DeptCascader.vue";
import CommonUpload from "@/components/Common/CommonUpload.vue";
import LinkTo from "@/mixins/LinkTo";
import uploadMixin from "@/components/GenerateForm/uploadMixin";
import { getDeptTree } from "@/api/System/Department";
import {
  getPolicyDetail,
  getPolicySave,
} from "@/api/BackendManage/Index.js";
import { required } from "@/utils/eValidate";
export default {
  name: "User",
  mixins: [LinkTo,uploadMixin],
  components: { OperateCommon, DeptCascader,CommonUpload },
  data() {
    return {
      id: 0,
      lanmu:[{
            value: '政策法规',
            label: '政策法规'
            }, {
            value: '督查通报',
            label: '督查通报'
            }
        ],
      types: [{
          value: '综合类',
          label: '综合类'
        }, {
          value: '前期审批类',
          label: '前期审批类'
        }, {
          value: '招投标类',
          label: '招投标类'
        }, {
          value: '实施管理类',
          label: '实施管理类'
        }, {
          value: '验收后评价类',
          label: '验收后评价类'
        }],
      fileschange:[],
      form: {
        info_title: "",
        info_column: "",
        publish_unit: "",
        publish_date: "",
        type: "",
        t_news:"",
        // 0 正常 1 删除
        state: "0",
        file_type:".pdf",
      },
      rules: {
        info_title: [required],
        info_column: [required],
        type: [required],
        // t_news:[required]
      },
      deptTree: [],
      roles: [],
      title: [],
      leaderType: [],
      pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
      },
    };
  },
  methods: {
    initDept() {
      getDeptTree().then((res) => {
        if (res.code == 0) {
          this.deptTree = res.data;
        }
      });
    },
    initData() {
      getPolicyDetail({ id: this.id }).then((res) => {
        if (res.code == 0) {
          this.form = res.data;
        }
      });
    },
    changeDept(val) {
      this.form.dept_name = val.label;
      this.form.dept_id = val.value;
    },
    save() {
      // if(this.form.t_news===''){
      //   return this.$message({
      //     message: '需要上传的文件不能为空！',
      //     type: 'warning'
      //   });      
      // }
      let files = [...this.$store.state.files.unBindFiles,...this.fileschange];
      this.$refs.form.validate((valid) => {
        if (valid) {
          getPolicySave(this.form).then((res) => {
            if (res.code == 0) {
              this.$message.success("保存成功");
              this.checkFile(res.data);
              this.linkTo("/policyList");
            }
          });
        }
      });
    },
    allowEdit() {
      switch (parseInt(this.pageType)) {
        case 0:
        case 1:
        case 2:
          if (this.form.report_status >= 10) {
            return true;
          }
        case 999:
          return false;
        default:
          return false;
      }
    },
  },
  created() {
    this.id = this.$route.params.id;
    if (this.id > 0) {
      this.initData();
    }
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/base-form.scss";
.xing{
  position: absolute;
  color: red;
  top: 0px;
    left: -82px;
}
</style>
